programming4us
           
 
 
Programming

iOS SDK : Debugging (part 3) - NSZombieEnabled

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/17/2011 11:36:38 AM

NSZombieEnabled

When an object is deallocated, if there are any objects with a reference to the deallocated object, they are no longer referencing a valid object. Any messages sent to the deallocated object result in errors. Often, the error is rather cryptic. For instance, the following code fragment is obviously an error.

FooBar * myFooBar = [[FooBar alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:

myFooBar,nil];
[myFooBar dealloc];
[[myArray objectAtIndex:0] sayHello];

FooBar is allocated, initialized, and added to myArray. There are two references to myFooBar, so its retainCount is two. However, deallocating myFooBar makes both references invalid, pointing to deallocated memory space. The sayHello message is sent to the first object in myArray—the problem is that the object no longer exists. Although in this simple example it is easy enough to surmise the cause of the error message, in a real application, finding this type of error’s source is often difficult.

objc[1289]: FREED(id): message sayHello sent to freed

object=0x521a90 Program received signal: "EXC_BAD_INSTRUCTION".

Zombies help avoid this nebulous error, helping you track down an error’s source. You enable zombies by setting the NSZombieEnabled environment variable in Xcode. Then, when debugging the application, rather than releasing an object, the debugger creates a zombie object. The zombie knows its original identity before joining the undead. The result is that you usually receive a more descriptive error message.

2009-02-28 12:28:38.749 Zombie[1316:20b] *** -[FooBar sayHello]:
message sent to deallocated instance 0x52c6a0

Again, in this simple example, the difference is trivial; in a real-world project, the difference is not trivial. The following task illustrates using NSZombieEnabled.

Try This: Enabling Zombies

  1. Create a new View-based Application named Zombie.

  2. Create a new Objective-C class called FooBar.

  3. Create one method called helloThere (Listing 1). Don’t forget to put the method’s signature in FooBar’s interface (Listing 2).

    Listing 1. FooBar.m
    #import "FooBar.h"
    @implementation FooBar
    -(void) sayHello {
    NSLog(@"Hello there...");
    }
    -(void) dealloc {
    [super dealloc];
    }
    @end

    Listing 2. FooBar.h
    @interface FooBar : NSObject {
    }
    -(void) sayHello;
    @end

  4. Modify application:didFinishLaunchingWithOptions in ZombieAppDelegate (Listing 3). Don’t forget to import FooBar.

    Listing 3. ZombieAppDelegate.m
    #import "ZombieAppDelegate.h"
    #import "ZombieViewController.h"
    #import "FooBar.h"
    @implementation ZombieAppDelegate
    @synthesize window;
    @synthesize viewController;
    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    FooBar * myFooBar = [[FooBar alloc] init];
    NSMutableArray * myArray = [[NSMutableArray alloc]
    initWithObjects:myFooBar,nil];
    [myFooBar dealloc];
    [[myArray objectAtIndex:0] sayHello];

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
    }

    - (void)applicationWillTerminate:(UIApplication *)application {
    // Save data if appropriate.
    }

    -(void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
    }
    @end



  5. Click the Run button to build and debug the application. Notice the first time you run the application you may not get an error at all (even though we sent a message to a deallocated object). This is part of what makes finding and fixing errors like this so hard—if the memory didn’t happen to get used for anything else in the meantime, it might remain valid for an unpredictable amount of time. Even if you do get an error message, it is not all that descriptive (Listing 4). The debugger only knows the sayHello message was sent to an object already freed. The debugger doesn’t know the object’s identity. To change this, you must enable zombies.

    Listing 4. Debugger Console error logging when zombies are not enabled
    Attaching to program: '/Users/bward/Library/Application Support/iPhone
    Simulator/User/Applications/05688B53-AF22-4F21
    -95D3AFFE2682A6EA/Zombie.app/Zombie', process 1351.
    objc[1351]: FREED(id): message sayHello sent to freed object=0x521bd0
    Program received signal: "EXC_BAD_INSTRUCTION".

  6. In the pull-down menu next to the Run button, select Edit Active Scheme. Select Launch in the left column and then click the Arguments tab. This should let you edit the arguments passed to the app on launch (Figure 11).

    Figure 11. The Executable Info window
  7. Click the + and add the NSZombieEnabled variable to the environment variable list. Assign it the value YES and click OK.

  8. Run the app again. Now the error message is more descriptive (Listing 5).

Listing 5. Debugger Console output after zombies are enabled
2010-08-21 15:10:57.345 Zombie[16692:207] *** -[FooBar sayHello]:
message sent to deallocated instance 0x592b640


Other -----------------
- iOS SDK : Debugging (part1 )
- iOS SDK : Installing Applications on an iPhone
- Software Testing with Visual Studio Team System 2008 : Web Testing - Recording a test
- Software Testing with Visual Studio Team System 2008 : Unit testing web services & Code coverage unit test
- .NET Debugging : Introduction to the Tools - SOS & SOSEX
- .NET Debugging : CLR 4.0 - Synchronization & Interoperability
- iPhone Programming : Connecting to the Network - Getting Data from the Internet
- iPhone Programming : Connecting to the Network - Sending Email
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Check Results
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Read and Write Files
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Get Dates and Times
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Work with Text
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 2)
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 1) - Physical Constraints
- Parallel Programming with Microsoft Visual Studio 2010 : Task Parallelism - Unhandled Exceptions in Tasks
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Tasks
- jQuery 1.3 : DOM Manipulation - Moving elements
- .NET Debugging : Introduction to the Tools - .NET 2.0—Redistributable & .NET 2.0—SDK
- .NET Debugging : Managed Heap and Garbage Collection
- Context and Interception : Custom Component Services (part 3) - The Transaction Management Service
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us